﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interfaces.Entities;
using EntitiesDefinitions;

namespace UnitTestsViaMSTest.RepositoriesTests
{

    /// <summary>
    /// helper class which exposes methods to return fake collection of IUser and IRole objects
    /// </summary>
    internal sealed class CollectionsInitializer
    {
       
        public IList<IUser> usersCollection { get; set;}

        /// <summary>
        /// Helper method that simulates returning of result collection
        /// </summary>
        /// <param name="returnFake">if false return collection of 3 elements, if true return empty collection</param>
        /// <returns>collection of Roles</returns>
        internal IList<IRole> GetRolesCollection(bool returnFake)
        {
           
            IList<IRole> roles = new List<IRole>();
            if (false == returnFake)
            {
                roles.Add(new Roles { ID = 1, Name = "Administrator" });
                roles.Add(new Roles { ID = 2, Name = "Report Builder" });
                roles.Add(new Roles { ID = 3, Name = "Invoice manager" });
            }
            return roles;
        }

        /// <summary>
        /// get an Role instance from the fake collection
        /// </summary>
        /// <param name="id">id of the objct</param>
        /// <returns>Role instance</returns>
        internal IRole GetFakeRoleObjectById(int id)
        {
            return GetRolesCollection(false).Where(role => role.ID == id).FirstOrDefault();
        }

        internal IList<IUser> GetUsersCollection(bool returnFake)
        {
            IList<IUser> users = new List<IUser>();

            if (false == returnFake)
            {
                users.Add(new Users { ID = 1, Hash = string.Empty, Salt = string.Empty, UserName = "kbochevski", Role = GetFakeRoleObjectById(1) });
                users.Add(new Users { ID = 2, Hash = string.Empty, Salt = string.Empty, UserName = "administrator", Role = GetFakeRoleObjectById(1) });
                users.Add(new Users { ID = 3, Hash = string.Empty, Salt = string.Empty, UserName = "reportManager", Role = GetFakeRoleObjectById(3) });
                users.Add(new Users { ID = 4, Hash = string.Empty, Salt = string.Empty, UserName = "noRoleUser", Role = null});

            }

            usersCollection = users; //assign the collection property in order to test Add, Delete, SaveChange methods

            return users;
        }

        internal IUser GetFakeUserById(int id)
        {
            return GetUsersCollection(false).Where(user => user.ID == id).FirstOrDefault();
        }

        internal IUser GetFakeUserByName(string name)
        {
            return GetUsersCollection(false).Where(user => user.UserName.Equals(name)).FirstOrDefault();
        }

    }

    public abstract class DBFakeContext : DataAccess.AuthenticationDemoEntities, IDBContext
    {
        public DBFakeContext()
            : base("metadata=res://*/ProjectContext.csdl|res://*/ProjectContext.ssdl|res://*/ProjectContext.msl;provider=System.Data.SqlClient;provider connection string='Data Source=HURBATA\\SQL2005;Initial Catalog=IoCDemo;Integrated Security=True;MultipleActiveResultSets=True'")
        { }
          public abstract new IList<IRole> Roles { get; set; }
          public abstract new IList<IUser> Users { get; set; }
          public abstract void RemoveObject(object entity);
          public abstract void AddNewObject(string entitySet, object entity);
        
    }

    public class Users : IUser
    {
        private int id;
        private string username;
        private string hash;
        private string salt;
        private IRole role;

        #region IUser Members

        public int ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

        public string UserName
        {
            get
            {
                return username;
            }
            set
            {
                username = value;
            }
        }

        public string Hash
        {
            get
            {
                return hash;
            }
            set
            {
                hash = value;
            }
        }

        public string Salt
        {
            get
            {
                return salt;
            }
            set
            {
                salt = value;
            }
        }

        public IRole Role
        {
            get
            {
                return role;
            }
            set
            {
                role = value;
            }
        }

        #endregion
    }
    public class Roles : IRole
    {
        private int Id;
        private string name;
        #region IRole Members

        public int ID
        {
            get
            {
                return Id;
            }
            set
            {
                Id = value;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        #endregion
    }
}
